package work.managercenter;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import utils.MyUtils;
import utils.Pair;

public class BauxiteCategoryParser {

	public static void main(String[] args) throws IOException {
		String fileLocation = "/home/gmarcone/bauxiteversions.txt";

		List<Pair<String, String>> list = parseBauxiteCategoriesFromFile(fileLocation);

		for (Pair<String, String> pair : list) {
			System.out.println(pair);
		}
	}

	private static List<Pair<String, String>> parseBauxiteCategoriesFromFile(String fileLocation) throws IOException {
		String showVersions = "showVersions";

		String bauxiteCategoriesInput = MyUtils.readFileToString(new File(fileLocation));

		String iter = bauxiteCategoriesInput.substring(bauxiteCategoriesInput.indexOf(showVersions));

		List<Pair<String, String>> list = new ArrayList<Pair<String,String>>();

		while (iter.indexOf(showVersions) >= 0) {
			iter = iter.substring(iter.indexOf(showVersions));
			String functionCall = iter.substring(iter.indexOf(showVersions), iter.indexOf(")") + 1);

			String categoryName = functionCall.substring(functionCall.indexOf("'") + 1, functionCall.indexOf(",") - 1);
			String categoryId = functionCall.substring(functionCall.indexOf(",") + 1, functionCall.length() - 1);

			list.add(new Pair<String, String>(categoryId, categoryName));
			iter = iter.substring(iter.indexOf(")") + 1);
		}

		return list;
	}
}
